package java.socket_3;



import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    Socket socket;
    Client() {
        try {
            System.out.println("已经启动客户端");
            System.out.println("正在链接服务器");
            //申请一个端口
            socket=new Socket("localhost",8808);
            System.out.println("成功链接服务器");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    public void start(){
        try {
            //创建低级输出流链接socket
            OutputStream os =socket.getOutputStream();
            //接入文本输出流 规定编码UTF-8
            OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
            //缓冲输出流接入文本输出流
            BufferedWriter bw = new BufferedWriter(osw);
            //接入行文本输出流
            PrintWriter pw = new PrintWriter(bw,true);
            //创建一个扫描器
            Scanner scanner = new Scanner(System.in);
            //创建一个字符串接受扫描器
            String line= scanner.nextLine();
            while (true) {
                if ("exit".equals(scanner.nextLine())) {//判断exit为退出条件
                    break;
                }
                pw.println(line);//输出

    }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        //创建一个客户端
        Client client = new Client();
        //执行客户端
        client.start();
    }
}
